<?php
/*
    OpenLib
    Copyright (C) 2011  Michael Gukov, Pham Thi Thuy Duong

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/


namespace App\Views\Reader;

use Sun\Html;
use Sun\Utils;
use App\Models;

class Order extends Main {
    public function __construct() {
        parent::__construct();
        $this->data_scene = 'OrderList';
    }


    public function GetSelItemsTableScene() {
        echo $this->GetSelItemsTable()->ToJSON();
    }

    public function OrderListTableScene() {
        echo $this->OrderList()->ToJSON();
    }


    public function OrderList() {
        $user = new Models\User();
        $sess = new Models\Session();
        $edit = new Models\Edition();

        $orders = $user->GetOrders($sess->UserId());

        $rows = array();
        for ($i = 0; $i < count($orders); ++$i) {
            $rows[] = array($i + 1, $edit->GetEditCatalogBrief($orders[$i]['edit_id']));
        }

        $table = new Html\QueryTable($rows, array("№", "Описание"));
        $table->id = "OrderTable";
        $table->style = "width: 100%;";

        $rows = $table->First()->Childs();

        for ($i = 0; $i < count($rows); ++$i) {
            $id = $i ? $orders[$i - 1]['order_id'] : "";

            $rows[$i]->First()->SetAttribute('class', 'ControlCell');

            $rows[$i]->AddData()->Added()->SetAttribute("class", "ControlCell")
                ->Add(new Html\A(array('href' => '?', 'data-id' => $id)))->Added()
                ->Add(new Html\Img(array(
                    'src' => "pub/media/images/clear_16.png",
                    'class' => "NoneBorder",
                    'alt' => 'Удалить')));
        }

        ////////////////// javascript //////////////////////
        $src = Utils::LoadFileToString(dirname(__FILE__)."/Order/OrderList.js");
        $script = new Html\Script($src, array('id' => "OrderListReaderScript"));
        ////////////////////////////////////////////////////

        $div = new Html\Div();
        $div->Add($table);
        $div->Add($script);

        return $div;
    }

    public function GetSelItemsTable() {
        $edit = new Models\Edition();

        $items = Models\Search::GetSelItems();
        $rows = array();

        for ($i = 0; $i < count($items); ++$i) {
            $rows[] = array($i + 1, $edit->GetEditCatalogBrief($items[$i]));
        }

        $table = new Html\QueryTable($rows, array("№", "Описание"));
        $table->id = "SelItemsTable";
        $table->style = "width: 100%;";

        $rows = $table->First()->Childs();

        for ($i = 0; $i < count($rows); ++$i) {
            $id = $i ? $items[$i - 1] : "";

            $rows[$i]->First()->SetAttribute('class', 'ControlCell');

            $rows[$i]->AddData()->Added()->SetAttribute("class", "ControlCell")
                ->Add(new Html\A(array('href' => '?', 'data-id' => $id)))->Added()
                ->Add(new Html\Img(array(
                    'src' => "pub/media/images/clear_16.png",
                    'class' => "NoneBorder",
                    'alt' => 'Отчистить')));
        }

        ////////////////// javascript //////////////////////
        $src = Utils::LoadFileToString(dirname(__FILE__)."/Order/Order.js");
        $script = new Html\Script($src, array('id' => "SelItemsTableScript"));
        ////////////////////////////////////////////////////

        $div = new Html\Div();
        $div->Add($table);
        $div->Add($script);

        return $div;
    }

}
